package cz.idsv.web.mvc.controller;

/**
 * ProductTypeController.
 * 
 * @author Jan Borzensky
 */
import java.util.Collection;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.idsv.web.mvc.model.entity.ProductTypeEntity;
import cz.idsv.web.mvc.model.service.ProductTypeService;



/**
 * ProductType controller.
 * 
 * @author Jan Borzensky
 */
@Controller
@SessionAttributes(value = {"productTypeEntity"})
public class ProductTypeController {
	
	private static final String LIST_MODEL_KEY = "list";
	private static final String PRODUCTTYPE_ENTITY = "productTypeEntity";
	
	protected static Logger logger = Logger.getLogger("ProductTypeController"); 
	
	@Autowired()
	private ProductTypeService productTypeService;
	
	/**
     * <p>Searches for all productType and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/productType/list'.</p>
     */
    @RequestMapping(value="/productType/list", method=RequestMethod.GET)
    public @ModelAttribute(LIST_MODEL_KEY) Collection<ProductTypeEntity> search() {
    	logger.debug("Received request to show all productType"); 
    	
        return productTypeService.findProductType();
    }
    
	/**
     * <p>Create new or update old productType 
     * <code>ProductTypeEntity</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/productType/form'.</p>
     */
    @RequestMapping(value="/productType/form", method=RequestMethod.GET)
    public @ModelAttribute(PRODUCTTYPE_ENTITY)  ProductTypeEntity getDocumentForm(@RequestParam(value = "id", required = false) Integer id) {
        if(id==null)return new ProductTypeEntity();
        return productTypeService.findProductTypeById(id);
    }
    
	/**
     * <p>Save new or update old productType 
     * <code>ProductEntity</code>.</p>
     * 
     * <p>Expected HTTP POST and response '/productType/saveForm'.</p>
     */
    @RequestMapping(value="/productType/saveForm", method=RequestMethod.POST)
    public String saveProductTypeForm(ProductTypeEntity productTypeEntity, Model model) {
    	if(productTypeEntity != null) 
    		productTypeService.save(productTypeEntity);
    	return "redirect:/productType/list.html";
    }
    
    /**
     * <p>Deletes a productType.</p>
     * 
     * <p>Expected HTTP POST and request '/productType/delete'.</p>
     */
    @RequestMapping(value="/productType/delete", method=RequestMethod.POST)
    public String delete(ProductTypeEntity productTypeEntity) {
    	logger.debug("Received request to delete productType with id:"+ productTypeEntity.getId());
        productTypeService.delete(productTypeEntity);
        return "redirect:/productType/list.html";
    }
    
}
